package cn.wit.testalgorithms;

/**
 * 种花问题
 * 
 * @author 16604
 *
 */
public class DemoFlowersProblem {
	public static void main(String[] args) {
		int[] flowered = { 1, 0, 0, 0, 0, 1 };
		int[] flowerbed = { 1, 0, 0, 0, 1 };
		int[] flowerbed1 = {  0 };
		int n = 2;
		// int n = 1;
		// System.out.println(canPlaceFlowers(flowered, n));
		// System.out.println(canPlaceFlowers(flowerbed, n));
		System.out.println(canPlaceFlowers(flowerbed1, n));

	}

	public static boolean canPlaceFlowers(int[] flowerbed, int n) {

		int count = 0;
		
		//判断开头
		if (flowerbed[0] == 0) {
			if (flowerbed.length == 1) {
				count++;
				//输出判断
				if (count >= n) {
					return true;
				} else {
					return false;
				}
			}
			else if (flowerbed[1] == 0) {
				flowerbed[0] = 1;
				count++;
			}
		}

		// 先记录能种多少花
		for (int i = 1; i < flowerbed.length - 1; i++) {
			if (flowerbed[i] == 0) {
				if (flowerbed[i + 1] == 0 && flowerbed[i - 1] == 0) {
					count++;
					// 每次已经种了，那就这里把它作为1
					flowerbed[i] = 1;
					System.out.println(count);
				}
			}
		}
		//判断结尾
		if (flowerbed[flowerbed.length - 1] == 0) {
			if (flowerbed[flowerbed.length - 2] == 0) {
				count++;
				flowerbed[flowerbed.length - 1] = 1;
			}
		}
		
		//输出判断
		if (count >= n) {
			return true;
		} else {
			return false;
		}
	}

}
